﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Reflection.Emit;

using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swcommands;
using SolidWorks.Interop.swconst;
using SolidWorks.Interop.swpublished;

using SwAddinHelpers.Contracts;
using SwAddinHelpers.Attributes;

namespace SwAddinHelpers.MenuManager
{
    public sealed class MenuManagerHost: AddinProxy
    {
        public MenuManagerHost(ISwAddinBase owner)
        {
            _OwnerAddin = owner;
        }

        public void Build()
        {
            IEnumerable<IExternalCallback> externalCallbackContainers =
               _OwnerAddin.compositionContainer.GetExportedValues<IExternalCallback>();

            foreach (IExternalCallback callbackContainer in externalCallbackContainers)
            {
                MethodInfo[] mis = callbackContainer.GetType().GetMethods();

                foreach (MethodInfo mi in mis)
                {
                    MenuItemCallbackAttribute[] mica =
                        (MenuItemCallbackAttribute[])mi.GetCustomAttributes(typeof(MenuItemCallbackAttribute), false);

                    if (mica.Length > 0)
                    {
                        _OwnerAddin.CallbackPool.InjectMenuProxyMethod(mi);

                        if (mica[0].menuItemEnableMethod != null)
                        {
                            MethodInfo enableMi = callbackContainer.GetType().GetMethod(mica[0].menuItemEnableMethod);
                            if (enableMi != null)
                            {
                                _OwnerAddin.CallbackPool.InjectMenuProxyMethod(enableMi);
                            }
                        }
                    }
                }
            }
        }

        public void PostBuild()
        {
            IEnumerable<IExternalCallback> externalCallbackContainers = 
                _OwnerAddin.compositionContainer.GetExportedValues<IExternalCallback>();

            foreach (IExternalCallback callbackContainer in externalCallbackContainers)
            {
                MethodInfo[] mis = callbackContainer.GetType().GetMethods();
                
                foreach (MethodInfo mi in mis)
                {
                    MenuItemCallbackAttribute[] mica =
                        (MenuItemCallbackAttribute[])mi.GetCustomAttributes(typeof(MenuItemCallbackAttribute), false);

                    if (mica.Length > 0)
                    {
                        string menuName = mica[0].menuItemName;

                        if((int)mica[0].parentMenu > -1 && mica[0].menuHasParent)
                        {
                            menuName += "@" + _OwnerAddin.SolidWorksApplication.GetLocalizedMenuName((int)mica[0].parentMenu);
                        }

                        foreach (swDocumentTypes_e _e in mica[0].documentTypes)
                        {
                            _OwnerAddin.SolidWorksApplication.AddMenuItem4(
                                (int)_e,
                                _OwnerAddin.Cookie,
                                menuName,
                                mica[0].menuItemPosition,
                                mi.Name,
                                mica[0].menuItemEnableMethod,
                                mica[0].menuItemToolTip,
                                null);
                             
                        }

                    }
                }

            }


            /*
            string localHelpMenu = _OwnerAddin.SolidWorksApplication.GetLocalizedMenuName(6);
            string localToolsMenu = _OwnerAddin.SolidWorksApplication.GetLocalizedMenuName(4);
            string menuItemHelp = "GAF-R2 Help@" + localHelpMenu;
            string menuItemAbout = "GAF-R2 About@" + localHelpMenu;
            string menuItemTools =  "GAF-R2 Manager@" + localToolsMenu;
            foreach (swDocumentTypes_e _e in new swDocumentTypes_e[] { swDocumentTypes_e.swDocASSEMBLY, swDocumentTypes_e.swDocDRAWING, swDocumentTypes_e.swDocPART, swDocumentTypes_e.swDocNONE })
            {
                _OwnerAddin.SolidWorksApplication.AddMenuItem4((int)_e, _OwnerAddin.Cookie, menuItemHelp, 3, "Menu1Callback", "Menu1Enable", null, null);
                _OwnerAddin.SolidWorksApplication.AddMenuItem4((int)_e, _OwnerAddin.Cookie, menuItemAbout, -1, "Menu2Callback", "Menu2Enable", null, null);
                _OwnerAddin.SolidWorksApplication.AddMenuItem4((int)_e, _OwnerAddin.Cookie, menuItemTools, 4, "Menu3Callback", "Menu3Enable", null, null);
            }
            */
        }

        public void Destroy()
        {
            // TODO: implement remove menu
            //_OwnerAddin.SolidWorksApplication.RemoveMenu(xxx);
        }  
    }
}